/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ClassesJava;

import connexion.ConnectionManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mariam
 * @author ISMAYLOULABBIHI
 */
public class Filiere {

    public ConnectionManager c;
    private int num;
    private String nom;
    private String niveau;

    public Filiere(int num, String nom, String niveau) {
        this.num = num;
        this.nom = nom;
        this.niveau = niveau;

    }

    public Filiere() {
      //To change body of generated methods, choose Tools | Templates.

    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getNiveau() {
        return niveau;
    }

    public void setNiveau(String niveau) {
        this.niveau = niveau;
    }

    //ajouter data

    public int ajouter(Filiere f) {
        int i;
        c = new ConnectionManager();
        String rq = "INSERT INTO  `gestion_des_emplois`.`filiere` (id , nom , niveau )"
                + " VALUES (NULL ,'" + f.getNom() + "', '" + f.getNiveau() + "')";
        i = c.setDataBase(rq);
        return i;
    }

    //Modifier data

    public int modifier(Filiere f) {
        int i = 0;
        c = new ConnectionManager();
        String rq = "UPDATE  `gestion_des_emplois`.`filiere` SET `nom` = '" + f.getNom() + "', "
                + "`niveau` = '" + f.getNiveau() + "' where id=" + f.getNum();
        i = c.setDataBase(rq);
        return i;
    }

    //supprimer data

    public int supprimer(Filiere f) {
        int i = 0;
        c = new ConnectionManager();
        String rq = "DELETE FROM `gestion_des_emplois`.`filiere` where id=" + f.getNum();
        i = c.setDataBase(rq);
        return i;
    }

    //afficher data

    public Vector<Filiere> getFilieres() {
        Vector<Filiere> res = new Vector();
        try {
            c = new ConnectionManager();
            ResultSet rs = c.getDataBase("select * from filiere");

            while (rs.next()) {
                Filiere f = new Filiere(rs.getInt(1), rs.getString(2), rs.getString(3));
                res.add(f);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Filiere.class.getName()).log(Level.SEVERE, null, ex);
        }

        return res;
    }
    //fonction qui return l'id de  Derniere filiere enregistre
  public int getDerniereFiliere() {
        int idDer;
        Filiere f = null ;
        try {
            c = new ConnectionManager();
            ResultSet rs = c.getDataBase("SELECT * FROM  `filiere` WHERE id = ( SELECT MAX( id ) FROM  `filiere` )");

            while (rs.next()) {
                 f = new Filiere(rs.getInt(1), rs.getString(2), rs.getString(3));
            }

        } catch (SQLException ex) {
            Logger.getLogger(Filiere.class.getName()).log(Level.SEVERE, null, ex);
        }

        return f.getNum() ;
    }
}
